最⻓定差⼦序列（medium）： https://leetcode.cn/problems/longest-arithmetic-subsequence-of-given-difference/
class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        // 创建⼀个哈希表
        unordered_map<int, int> hash; // {arr[i], dp[i]}
        hash[arr[0]] = 1;             // 初始化
        int ret = 1;
        for (int i = 1; i < arr.size(); i++) {
            hash[arr[i]] = hash[arr[i] - difference] + 1;
            ret = max(ret, hash[arr[i]]);
        }
        return ret;
    }
};

最⻓的斐波那契⼦序列的⻓度（medium）： https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/
class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        int n = arr.size();
        // 优化
        unordered_map<int, int> hash;
        for (int i = 0; i < n; i++)
            hash[arr[i]] = i;
        int ret = 2;
        vector<vector<int>> dp(n, vector<int>(n, 2));
        for (int j = 2; j < n; j++) // 固定最后⼀个位置
        {
            for (int i = 1; i < j; i++) // 固定倒数第⼆个位置
            {
                int a = arr[j] - arr[i];
                // 条件成⽴的情况下更新
                if (a < arr[i] && hash.count(a))
                    dp[i][j] = dp[hash[a]][i] + 1;
                ret = max(ret, dp[i][j]); // 统计表中的最⼤值
            }
        }
        return ret < 3 ? 0 : ret;
    }
};